package com.mybatis.example.dao;

import com.mybatis.example.dao.utils.MyBatisUtils;
import com.mybatis.example.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;

public class testUserMapper {
    @Test
    public void testGetUsers(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.getUsers();
        for (User user : users) {
            System.out.println(user);
        }
        sqlSession.close();
    }

    @Test
    public void testCache1(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        HashMap<String, Object> map = new HashMap<>();
        map.put("email","updateEmail@test.com");
        map.put("id",8);

        User user = mapper.getUserById(5);
        System.out.println(user);
        System.out.println("========");
        //mapper.updateEmailById(map);
        //sqlSession.clearCache();
        System.out.println(mapper.getUserById(5));

        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void testCache2(){
        SqlSession sqlSession1 = MyBatisUtils.getSqlSession();
        SqlSession sqlSession2 = MyBatisUtils.getSqlSession();

        UserMapper mapper1 = sqlSession1.getMapper(UserMapper.class);
        User user1 = mapper1.getUserById(2);
        System.out.println(user1);
        sqlSession1.close();

        System.out.println("--------");
        UserMapper mapper2 = sqlSession2.getMapper(UserMapper.class);
        User user2 = mapper2.getUserById(2);
        System.out.println(user2);
        System.out.println(user1 == user2);

        sqlSession2.close();


    }
}
